// myself/pages/my-training/detail/detail.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hasStudyPower:false,
    hasTrainPower:false,
    hasExamPower:false,
    CDNPATH: app.globalData.CDNPATH,
    coachingSessionId: '',
    coachDetail: {},
    mainHidden: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const { coachingSessionId } = options
    const permissionCodes = app.globalData.permissionCodes || []
    this.setData({
      coachingSessionId,
      hasStudyPower:permissionCodes.includes('train_study'),
      hasTrainPower:permissionCodes.includes('rehearse'),
      hasExamPower:permissionCodes.includes('customs_clearance'),
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getDetail()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getDetail () {
    const { coachingSessionId } = this.data
    app.api.get({
      url: 'coaching_vx_student_session_detail',
      data: { coachingSessionId }
    }).then(res => {
      this.setData({
        coachDetail: res
      })
    })
  },
  checkRanking () {
    const { coachingSessionId } = this.data
    wx.navigateTo({
      url: `/myself/pages/my-training/rank/rank?coachingSessionId=${coachingSessionId}&sensors_ext=05-02`
    })
  },
  toCourseDetail (e) {
    const { activityStatus } = this.data.coachDetail
    if (activityStatus === 'notStart' || activityStatus === 'finished') {
      this.disabledClick()
      return
    }
    const { coachingSessionId } = this.data
    const { courseId: id, coachingContentId } = e.currentTarget.dataset.item
    wx.navigateTo({
      url: `/pages/learn/course-detail/courseDetail?id=${id}&sensors_ext=05-02&from=coaching&coachingContentId=${coachingContentId}&coachingSessionId=${coachingSessionId}`,
    })
  },
  disabledClick () {
    console.log('111111');
    const { activityStatus } = this.data.coachDetail
    let word = ''
    switch (activityStatus) {
      case 'notStart':
        word = '考试活动暂未开始'
        break;
      case 'finished':
        word = '考试活动已结束'
        break;
      default:
        break;
    }
    wx.showToast({
      title: word,
      icon: 'none',
      duration: 1000,
    })
  },
  toggleMain () {
    this.setData({
      mainHidden: !this.data.mainHidden
    })
  }
})